package com.jie.controller;
import com.jie.dto.UploadImageDTO;
import com.jie.po.rep.FileUploadResponse;
import com.jie.po.rep.MultipartUploadCreateResponse;
import com.jie.po.req.CompleteMultipartUploadRequest;
import com.jie.po.req.MultipartUploadCreateRequest;
import com.jie.response.RespBean;
import com.jie.service.impl.FileService;
import com.jie.store.context.UploadStrategyContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/file")
@Api(tags = "文件上传")
@RequiredArgsConstructor
public class FileController {

    private final UploadStrategyContext uploadStrategyContext;
    private final FileService fileService;

    @ApiOperation(value = "上传图片", notes = "上传图片,上传后返回原图和缩略图的url")
    @PostMapping("/image/upload")
    public RespBean<UploadImageDTO> uploadImage(MultipartFile file) {
        return RespBean.success("缩略图",uploadStrategyContext.executeUploadStrategyImages(file));
    }

    @ApiOperation(value = "上传文件", notes = "上传文件，上传后返回文件url")
    @PostMapping("/file/upload")
    public RespBean<String> uploadFile(MultipartFile file) {
        return RespBean.success("文件url",uploadStrategyContext.executeUploadStrategy(file));
    }
    @ApiOperation("创建分片上传 ")
    @PostMapping("/multipart/create")
    public MultipartUploadCreateResponse createMultipartUpload(
            @RequestBody
            @Validated
            MultipartUploadCreateRequest multipartUploadCreateRequest
    ) {
        return fileService.createMultipartUpload(multipartUploadCreateRequest);
    }

    @ApiOperation("合并分片")
    @PostMapping("/multipart/complete")
    public FileUploadResponse completeMultipartUpload(
            @RequestBody
            @Validated
            CompleteMultipartUploadRequest uploadRequest
    ) {
        return fileService.completeMultipartUpload(uploadRequest);
    }
}